package com.ibiaoke.boss.controller;

import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.LocationUtils;
import com.ibiaoke.system.dao.ShopDao;
import com.ibiaoke.system.entity.Shop;
import com.ibiaoke.system.entity.vo.XcxShopDetail;
import com.ibiaoke.system.service.ShopService;
import com.ibiaoke.system.service.XcxShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class XcxShopController {

    @Autowired
    private XcxShopService xcxShopService;

    @Autowired
    private ShopDao shopDao;

    /**
     * 小程序获取门店信息，根据距离远近排序
     * @param longitude
     * @param latitude
     * @return
     */
    @RequestMapping("/XcxListSortedShops")
    @ResponseBody
    public Res xcxListSortedShops(Double longitude, Double latitude) {
        /*List listSortList = xcxShopService.listSortedShops(longitude, latitude);
        return new Res().addResponse("listSortShops", listSortList);*/

        //测试   2018-04-27
        List<XcxShopDetail> XcxShopDetailList = new ArrayList<XcxShopDetail>();
        Shop shop = shopDao.getShopByShopId(1011);
        XcxShopDetail shopDetail =new XcxShopDetail();
        shopDetail.setId(shop.getShopId());
        shopDetail.setName(shop.getShopName());
        shopDetail.setBusinessTime(shop.getOpenBeginTime()+" - "+shop.getOpenEndTime().toString());
        Double distince = LocationUtils.getDistance(longitude, latitude, shop.getDesLongitude(), shop.getDesLatitude());
        DecimalFormat df = new DecimalFormat("######0.0");
        shopDetail.setDistince(Double.valueOf(df.format(distince/1000)));
        shopDetail.setShopowner(shop.getContactPerson());
        shopDetail.setMobile(shop.getShopMobile());
        shopDetail.setShopAddress(shop.getShopAddress());
        shopDetail.setDesLatitude(shop.getDesLatitude());
        shopDetail.setDesLongitude(shop.getDesLongitude());
        shopDetail.setStatus(shopDetail.getDistince() >= 15 ? 0 : 1);
        XcxShopDetailList.add(shopDetail);
        return new Res().addResponse("listSortShops", XcxShopDetailList);
    }

    /**
     * 小程序获取门店信息，根据距离远近排序,显示3公里内的最近店铺
     * @param longitude
     * @param latitude
     * @return
     */
    @RequestMapping("/getNearXcxShop")
    @ResponseBody
    public Res getNearXcxShop(Double longitude, Double latitude) {
        /*List<XcxShopDetail> listSortList = xcxShopService.listSortedShops(longitude, latitude);
        XcxShopDetail nearShop = xcxShopService.getNearXcxShop(listSortList);
        return new Res().addResponse("nearShop", nearShop);*/

        //测试   2018-04-27
        Shop shop = shopDao.selectByPrimaryKey(1011);
        XcxShopDetail shopDetail =new XcxShopDetail();
        shopDetail.setId(shop.getShopId());
        shopDetail.setName(shop.getShopName());
        shopDetail.setBusinessTime(shop.getOpenBeginTime()+" - "+shop.getOpenEndTime().toString());
        Double distince = LocationUtils.getDistance(longitude, latitude, shop.getDesLongitude(), shop.getDesLatitude());
        DecimalFormat df = new DecimalFormat("######0.0");
        shopDetail.setDistince(Double.valueOf(df.format(distince/1000)));
        shopDetail.setShopowner(shop.getContactPerson());
        shopDetail.setMobile(shop.getShopMobile());
        shopDetail.setShopAddress(shop.getShopAddress());
        shopDetail.setDesLatitude(shop.getDesLatitude());
        shopDetail.setDesLongitude(shop.getDesLongitude());
        shopDetail.setStatus(shopDetail.getDistince() >= 15 ? 0 : 1);
        return new Res().addResponse("nearShop", shopDetail);
    }

    @RequestMapping("/getShopById")
    @ResponseBody
    public Res getShopById(Integer shopId) {
        Shop shop = xcxShopService.getShpopById(shopId);
        return new Res().addResponse("shop", shop);
    }
}